namespace ASPNET.StarterKit.IssueTracker
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class DisplayIssues : UserControl
    {
        private string[] _arrIssueColumns;
        [AccessedThroughProperty("btnCancel")]
        private Button _btnCancel;
        [AccessedThroughProperty("btnSave")]
        private Button _btnSave;
        private IssueCollection _DataSource;
        [AccessedThroughProperty("grdIssues")]
        private DataGrid _grdIssues;
        [AccessedThroughProperty("lblNoIssues")]
        private Label _lblNoIssues;
        [AccessedThroughProperty("lnkSelectColumns")]
        private LinkButton _lnkSelectColumns;
        [AccessedThroughProperty("lstIssueColumns")]
        private CheckBoxList _lstIssueColumns;
        [AccessedThroughProperty("pnlSelectColumns")]
        private Panel _pnlSelectColumns;
        [AccessedThroughProperty("tblOptions")]
        private HtmlTable _tblOptions;
        private object designerPlaceholderDeclaration;

        public event EventHandler RebindCommand;

        public DisplayIssues()
        {
            base.Init += new EventHandler(this.Page_Init);
            this._arrIssueColumns = new string[] { "3", "4", "5", "6", "7", "8" };
        }

        public void CancelClick(object s, EventArgs e)
        {
            this.pnlSelectColumns.Visible = false;
        }

        public override void DataBind()
        {
            this.SortGridData(this._DataSource, this.SortField, this.SortAscending);
            this.grdIssues.DataSource = this._DataSource;
            this.grdIssues.DataBind();
            if (this.grdIssues.Items.Count > 0)
            {
                this.grdIssues.Visible = true;
                this.tblOptions.Visible = true;
                this.lblNoIssues.Visible = false;
                this.DisplayColumns();
            }
            else
            {
                this.grdIssues.Visible = false;
                this.tblOptions.Visible = false;
                this.lblNoIssues.Visible = true;
            }
        }

        public void DisplayColumns()
        {
            int _Vb_t_i4_0 = this.grdIssues.Columns.Count - 1;
            for (int index = 2; index <= _Vb_t_i4_0; index++)
            {
                this.grdIssues.Columns[index].Visible = false;
            }
            foreach (string colIndex in this._arrIssueColumns)
            {
                this.grdIssues.Columns[int.Parse(colIndex)].Visible = true;
            }
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        public void IssuesPageIndexChanged(object s, DataGridPageChangedEventArgs e)
        {
            this.grdIssues.CurrentPageIndex = e.NewPageIndex;
            this.OnRebindCommand(EventArgs.Empty);
        }

        public void IssuesSortCommand(object s, DataGridSortCommandEventArgs e)
        {
            this.SortField = e.SortExpression;
            this.OnRebindCommand(EventArgs.Empty);
        }

        private void lnkSelectColumns_Click(object sender, EventArgs e)
        {
        }

        public void OnRebindCommand(EventArgs e)
        {
            if (this.RebindCommand != null)
            {
                this.RebindCommand(this, e);
            }
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
            this.PageInit();
        }

        public void PageInit()
        {
            if ((this.Request.Cookies["issuecolumns"] != null) && (StringType.StrCmp(this.Request.Cookies["issuecolumns"].Value, string.Empty, false) != 0))
            {
                this._arrIssueColumns = this.Request.Cookies["issuecolumns"].Value.Split(new char[0]);
            }
        }

        public void SaveClick(object s, EventArgs e)
        {
            string strIssueColumns = " 0";
            foreach (ListItem item in this.lstIssueColumns.Items)
            {
                if (item.Selected)
                {
                    strIssueColumns = strIssueColumns + " " + item.Value;
                }
            }
            strIssueColumns = strIssueColumns.Trim();
            this._arrIssueColumns = strIssueColumns.Split(new char[0]);
            this.Response.Cookies["issuecolumns"].Value = strIssueColumns;
            this.Response.Cookies["issuecolumns"].Path = "/";
            this.Response.Cookies["issuecolumns"].Expires = DateTime.MaxValue;
            this.pnlSelectColumns.Visible = false;
            this.OnRebindCommand(EventArgs.Empty);
        }

        public void SelectColumnsClick(object s, EventArgs e)
        {
            this.pnlSelectColumns.Visible = true;
            foreach (string colIndex in this._arrIssueColumns)
            {
                ListItem item = this.lstIssueColumns.Items.FindByValue(colIndex);
                if (item != null)
                {
                    item.Selected = true;
                }
            }
        }

        private void SortGridData(IssueCollection list, string sortField, bool asc)
        {
            IssueCollection.IssueFields sortCol = IssueCollection.IssueFields.InitValue;
            string _Vb_t_string_0 = sortField;
            if (StringType.StrCmp(_Vb_t_string_0, "Id", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Id;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Title", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Title;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Category", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Category;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Assigned", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Assigned;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Owner", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Owner;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Creator", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Creator;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Priority", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Priority;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Status", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Status;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Milestone", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Milestone;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Created", false) == 0)
            {
                sortCol = IssueCollection.IssueFields.Created;
            }
            if (list != null)
            {
                list.Sort(sortCol, asc);
            }
        }

        protected virtual Button btnCancel
        {
            get
            {
                return this._btnCancel;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnCancel != null)
                {
                    this._btnCancel.Click -= new EventHandler(this.CancelClick);
                }
                this._btnCancel = value;
                if (this._btnCancel != null)
                {
                    this._btnCancel.Click += new EventHandler(this.CancelClick);
                }
            }
        }

        protected virtual Button btnSave
        {
            get
            {
                return this._btnSave;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnSave != null)
                {
                    this._btnSave.Click -= new EventHandler(this.SaveClick);
                }
                this._btnSave = value;
                if (this._btnSave != null)
                {
                    this._btnSave.Click += new EventHandler(this.SaveClick);
                }
            }
        }

        public int CurrentPageIndex
        {
            get
            {
                return this.grdIssues.CurrentPageIndex;
            }
            set
            {
                this.grdIssues.CurrentPageIndex = value;
            }
        }

        public IssueCollection DataSource
        {
            get
            {
                return this._DataSource;
            }
            set
            {
                this._DataSource = value;
            }
        }

        protected virtual DataGrid grdIssues
        {
            get
            {
                return this._grdIssues;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._grdIssues != null)
                {
                    this._grdIssues.SortCommand -= new DataGridSortCommandEventHandler(this.IssuesSortCommand);
                    this._grdIssues.PageIndexChanged -= new DataGridPageChangedEventHandler(this.IssuesPageIndexChanged);
                }
                this._grdIssues = value;
                if (this._grdIssues != null)
                {
                    this._grdIssues.SortCommand += new DataGridSortCommandEventHandler(this.IssuesSortCommand);
                    this._grdIssues.PageIndexChanged += new DataGridPageChangedEventHandler(this.IssuesPageIndexChanged);
                }
            }
        }

        protected virtual Label lblNoIssues
        {
            get
            {
                return this._lblNoIssues;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lblNoIssues != null)
                {
                }
                this._lblNoIssues = value;
                if (this._lblNoIssues != null)
                {
                }
            }
        }

        protected virtual LinkButton lnkSelectColumns
        {
            get
            {
                return this._lnkSelectColumns;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lnkSelectColumns != null)
                {
                    this._lnkSelectColumns.Click -= new EventHandler(this.lnkSelectColumns_Click);
                    this._lnkSelectColumns.Click -= new EventHandler(this.SelectColumnsClick);
                }
                this._lnkSelectColumns = value;
                if (this._lnkSelectColumns != null)
                {
                    this._lnkSelectColumns.Click += new EventHandler(this.lnkSelectColumns_Click);
                    this._lnkSelectColumns.Click += new EventHandler(this.SelectColumnsClick);
                }
            }
        }

        protected virtual CheckBoxList lstIssueColumns
        {
            get
            {
                return this._lstIssueColumns;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lstIssueColumns != null)
                {
                }
                this._lstIssueColumns = value;
                if (this._lstIssueColumns != null)
                {
                }
            }
        }

        protected virtual Panel pnlSelectColumns
        {
            get
            {
                return this._pnlSelectColumns;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._pnlSelectColumns != null)
                {
                }
                this._pnlSelectColumns = value;
                if (this._pnlSelectColumns != null)
                {
                }
            }
        }

        public bool SortAscending
        {
            get
            {
                object o = RuntimeHelpers.GetObjectValue(this.ViewState["SortAscending"]);
                return ((o == null) || BooleanType.FromObject(o));
            }
            set
            {
                this.ViewState["SortAscending"] = value;
            }
        }

        public string SortField
        {
            get
            {
                object o = RuntimeHelpers.GetObjectValue(this.ViewState["SortField"]);
                if (o == null)
                {
                    return string.Empty;
                }
                return StringType.FromObject(o);
            }
            set
            {
                if (StringType.StrCmp(value, this.SortField, false) == 0)
                {
                    this.SortAscending = !this.SortAscending;
                }
                this.ViewState["SortField"] = value;
            }
        }

        protected virtual HtmlTable tblOptions
        {
            get
            {
                return this._tblOptions;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._tblOptions != null)
                {
                }
                this._tblOptions = value;
                if (this._tblOptions != null)
                {
                }
            }
        }
    }
}

